package com.company.behavioral.state_pattern.context;

import com.company.behavioral.state_pattern.concrete_state.NormalState;
import com.company.behavioral.state_pattern.state.AccountState;

/**
 * 银行账户
 * @ClassName Account
 * @Author tiezheng.han
 * @Date 2020/9/7 13:47
 **/
public class Account {

    /**
     * 维持一个对抽象状态对象的引用
     */
    private AccountState state;
    /**
     * 开户名称
     */
    private String owner;
    /**
     * 账户余额
     */
    private double blanace = 0;

    public Account(String owner, double blanace) {
        // 设置初始状态
        this.state = new NormalState(this);
        this.owner = owner;
        this.blanace = blanace;
        System.out.println( this.owner + "开户，初始金额为" + blanace);
        System.out.println("------------------------------------------");
    }

    public double getBlanace() {
        return blanace;
    }

    public void setBlanace(double blanace) {
        this.blanace = blanace;
    }

    public void setState(AccountState state) {
        this.state = state;
    }

    public void deposit(double amount){
        System.out.println(this.owner + "存款" + amount);
        // 调用状态对象的deposit()方法
        state.deposit(amount);
        System.out.println("现在余额为" + this.blanace);
        System.out.println("现在账户状态为" + this.state.getClass().getName());
        System.out.println("------------------------------------------------");
    }

    public void withdraw(double amount){
        System.out.println(this.owner + "取款" + amount);
        // 调用状态对象的withdraw()方法
        state.withdraw(amount);
        System.out.println("现在余额为" + this.blanace);
        System.out.println("现在账户状态为" + this.state.getClass().getName());
        System.out.println("------------------------------------------------");
    }

    public void computeInsterst(){
        // 调用状态对象的computeInsterst()方法
        state.computeInterest();
    }
}
